package ext.acae2.change.ui;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import wt.change2.WTChangeActivity2;
import wt.change2.WTChangeOrder2;
import wt.doc.WTDocument;
import wt.enterprise.RevisionControlled;
import wt.fc.Persistable;
import wt.log4j.LogR;
import wt.part.WTPart;
import wt.util.WTException;
import wt.util.WTMessage;

import com.ptc.core.meta.common.TypeIdentifierHelper;
import com.ptc.core.ui.resources.FeedbackType;
import com.ptc.core.ui.validation.DefaultUIComponentValidator;
import com.ptc.core.ui.validation.UIValidationCriteria;
import com.ptc.core.ui.validation.UIValidationFeedbackMsg;
import com.ptc.core.ui.validation.UIValidationKey;
import com.ptc.core.ui.validation.UIValidationResult;
import com.ptc.core.ui.validation.UIValidationStatus;
import com.ptc.netmarkets.model.NmOid;

import ext.acae2.ACAEConstants;
import ext.acae2.change.helper.ChangeTaskHelper;
import ext.acae2.change.helper.ECNHelper;
import ext.acae2.change.resource.ECNResource;

/**
 * @desription edit change Task validator
 * @author cnie
 * @date 2013-5-15 上午12:03:29
 */
public class EditChangeTaskValidator extends DefaultUIComponentValidator {

	private static final Logger LOGGER = LogR
			.getLogger(EditChangeTaskValidator.class.getName());

	private static final String RESOURCE = ECNResource.class.getName();

	@Override
	public UIValidationResult validateFormSubmission(
			UIValidationKey uivalidationkey,
			UIValidationCriteria uivalidationcriteria, Locale locale)
			throws WTException {
		
		
		UIValidationResult uivalidationresult = UIValidationResult.newInstance(uivalidationkey, UIValidationStatus.PERMITTED);
		
		String errorStr = null;
		
//		String initItemOids = uivalidationcriteria.getTextParameter("initialRows_changeTask_resultingItems_table");
		
//		String rmItemOids = uivalidationcriteria.getTextParameter("rmRows_changeTask_resultingItems_table");
		
//		String addItemOids = uivalidationcriteria.getTextParameter("changeTask_resultingItems_table");
		
		List<NmOid> addItemList = uivalidationcriteria.getAddedItemsByName("changeTask_resultingItems_table");
		List<NmOid> initItemList = uivalidationcriteria.getInitialItemsByName("changeTask_resultingItems_table");
		List<NmOid> rmItemList = uivalidationcriteria.getRemovedItemsByName("changeTask_resultingItems_table");
		
		addItemList.addAll(initItemList);
		
		Persistable p = uivalidationcriteria.getSelectedRowObject().getObject();
		
		LOGGER.debug("EditChangeTaskValidator=========>"+p);

		//Persistable p =uivalidationcriteria.getContextObject().getObject();
		WTChangeOrder2 ecn = null;
		if(p instanceof WTChangeOrder2){
			ecn = (WTChangeOrder2)p;
		}else if(p instanceof WTChangeActivity2){
			WTChangeActivity2 ca = (WTChangeActivity2)p;
			ecn = ECNHelper.getChangeOrderByChangeActivity(ca);
		}
		String ecnType = TypeIdentifierHelper.getType(ecn).toExternalForm();
		if(rmItemList.containsAll(addItemList))
			errorStr = WTMessage.getLocalizedMessage(RESOURCE, ECNResource.NO_RESULT_ITEM_ERROR,null,locale);
		else{
			List<NmOid> resultItems = new ArrayList<NmOid>();
			for(NmOid nmoid:addItemList){
				if(!rmItemList.contains(nmoid))
					resultItems.add(nmoid);   //存在产生对象的对象信息
			}
			
			if(ecnType.indexOf(ACAEConstants.ACAE2_TYPE_FILE_ECN)>-1){
				if(resultItems.size()>1)
					errorStr = WTMessage.getLocalizedMessage(RESOURCE, ECNResource.RESULT_ITEM_COUNT_ERROR,null,locale);
				else{
					NmOid initNmoid = initItemList.get(0);
					WTDocument doc = (WTDocument) initNmoid.getWtRef().getObject();
					if(!resultItems.contains(initNmoid))
						errorStr = WTMessage.getLocalizedMessage(RESOURCE, ECNResource.NO_PRIMARY_DOC_RESVIED_ERROR,new Object[]{doc.getName()},locale);
				}
				
			}else if(ecnType.indexOf(ACAEConstants.ACAE2_TYPE_PARTDRAWING_ECN)>-1){
				WTPart rootPart = null;   //此次受影响对象主对象部件
				List<RevisionControlled> releatedItems = new ArrayList<RevisionControlled>(); //主对象part的关联的所有对象信息
				for(NmOid initNmoid:initItemList){
					if(initNmoid.isA(WTPart.class)){
						WTPart initPart = (WTPart) initNmoid.getWtRef().getObject();
						if(!releatedItems.contains(initPart)){
							rootPart = initPart;
							ChangeTaskHelper.getPrimaryRelectedCADDocs(initPart,releatedItems);
						}
					}
				}
				if(!resultItems.contains(rootPart))
					errorStr = WTMessage.getLocalizedMessage(RESOURCE, ECNResource.REMOVE_PRIMARY_OBJ_ERROR,new Object[]{rootPart.getName()},locale);
				else{
					boolean isPrimaryRelatedItem = true;
					for(RevisionControlled releatedRc:releatedItems){
						if(!releatedItems.contains(releatedRc))
							isPrimaryRelatedItem=false;break;
					}
					if(!isPrimaryRelatedItem)
						errorStr = WTMessage.getLocalizedMessage(RESOURCE, ECNResource.REVISE_PRIMARY_RELEATED_OBJ_ERROR,new Object[]{rootPart.getName()},locale);
				}
			}
		}
		
		if(StringUtils.isNotBlank(errorStr)){
			uivalidationresult.setStatus(UIValidationStatus.DENIED);
			uivalidationresult.addFeedbackMsg(UIValidationFeedbackMsg.newInstance(errorStr, FeedbackType.ERROR));
		}
		
		return uivalidationresult;
	}

}
